package com.tanhua.management.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.management.service.SystemService;
import com.tanhua.management.vo.ProfileVo;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RequestMapping("/system/users")
@RestController
public class SystemController {

    @Reference
    private LineCaptcha lineCaptcha;

    @Autowired
    private SystemService systemService;

    /**
     * 生成图片验证码
     * @param request
     * @param response
     */
    @GetMapping("/verification")
    @NoAuthorization
    public void ImgCode(HttpServletRequest request, HttpServletResponse response,@RequestParam("uuid") String uuid){

        lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        try {

            request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
            response.setContentType("image/png");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            lineCaptcha.write(response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 对验证码进行输入验证
     * @param vername
     * @return
     */
/*    @RequestMapping(value = "/getverfy",method = RequestMethod.POST)
    public @ResponseBody
    JSONObject verfy(String vername){
        JSONObject json =new JSONObject();
        if(lineCaptcha.verify(vername)){
            json.put("Verfystatus","Correct");
        }else {
            json.put("Verfystatus","Wrong");
        }
        return json;
    }*/

    /**
     * 对验证码进行输入验证
     * @param verificationCode
     * @return
     */
    @PostMapping ("login")
    @NoAuthorization
    public String getToken(@RequestParam("username")String username,
                        @RequestParam("password")String password,
                        @RequestParam("verificationCode")String verificationCode,
                        @RequestParam("uuid") String uuid){

        if(!lineCaptcha.verify(verificationCode)){

            return null;
        }
        //System.out.println("验证码正确");
        String token = systemService.getToken(username, password,uuid);

        return token;
    }

    /**
     * 获得用户信息
     *
     * @param
     * @return
     */
    @PostMapping (value = "/profile")
    public ProfileVo getAvatar(/*@RequestHeader("Authorization")String token*/){

        ProfileVo profileVo = this.systemService.queryByToken();

        return profileVo;
    }

    /**
     * 退出程序
     * @param
     */
    @PostMapping (value = "/logout")
    public String logout(HttpServletResponse response) {
        return "redirect://192.168.67.91:8080";
    }
}
